﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Markup;

namespace UIElements
{
    /// <summary>
    /// Follow steps 1a or 1b and then 2 to use this custom control in a XAML file.
    ///
    /// Step 1a) Using this custom control in a XAML file that exists in the current project.
    /// Add this XmlNamespace attribute to the root element of the markup file where it is 
    /// to be used:
    ///
    ///     xmlns:MyNamespace="clr-namespace:ArticleCreationControlesWPF"
    ///
    ///
    /// Step 1b) Using this custom control in a XAML file that exists in a different project.
    /// Add this XmlNamespace attribute to the root element of the markup file where it is 
    /// to be used:
    ///
    ///     xmlns:MyNamespace="clr-namespace:ArticleCreationControlesWPF;assembly=ArticleCreationControlesWPF"
    ///
    /// You will also need to add a project reference from the project where the XAML file lives
    /// to this project and Rebuild to avoid compilation errors:
    ///
    ///     Right click on the target project in the Solution Explorer and
    ///     "Add Reference"->"Projects"->[Browse to and select this project]
    ///
    ///
    /// Step 2)
    /// Go ahead and use your control in the XAML file.
    ///
    ///     <MyNamespace:CheckedComboBox/>
    ///
    /// </summary>
    public class CheckedComboBox : Control
    {
        public static readonly DependencyProperty ColorProperty;
        static CheckedComboBox()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(CheckedComboBox), new FrameworkPropertyMetadata(typeof(CheckedComboBox)));
            FrameworkPropertyMetadata md = new FrameworkPropertyMetadata();
            CheckedComboBox.ColorProperty = DependencyProperty.Register("Color",
            typeof(Color), typeof(CheckedComboBox), md);
        }

        public Color Color
        {
            get
            {
                return (Color)GetValue(CheckedComboBox.ColorProperty);
            }
            set
            {
                SetValue(CheckedComboBox.ColorProperty, value);
            }
        }

        public CheckedComboBox()
            : base()
        {
            //this.AddHandler(CheckBox.ClickEvent, new RoutedEventHandler(chkSelect_Click));

            Slider redSlider = (Slider)this.FindName("grid");
            Slider blueSlider = (Slider)this.FindName("blueSlider");
            Slider greenSlider = (Slider)this.FindName("greenSlider");
        }

    }


    [ValueConversion(typeof(double), typeof(Color))] 
    public class ColorToR : MarkupExtension, IValueConverter
    {
        private Color initialColor;
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return this;
        }
        object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            initialColor = (Color)value;
            return initialColor.R;
        }
        public object ConvertBack(object value, Type targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            Color c = initialColor;
            if (value != DependencyProperty.UnsetValue)
                c.R = System.Convert.ToByte(value);
            return c;
        }
    }

    [ValueConversion(typeof(double), typeof(Color))] 
    public class ColorToG : MarkupExtension, IValueConverter
    {
        private Color initialColor;
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return this;
        }
        object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            initialColor = (Color)value;
            return initialColor.G;
        }
        public object ConvertBack(object value, Type targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            Color c = initialColor;
            if (value != DependencyProperty.UnsetValue)
                c.G = System.Convert.ToByte(value);
            return c.ToString();
        }
    }

    [ValueConversion(typeof(double), typeof(Color))] 
    public class ColorToB : MarkupExtension, IValueConverter
    {
        private Color initialColor;
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return this;
        }
        object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            initialColor = (Color)value;
            return initialColor.B;
        }
        public object ConvertBack(object value, Type targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            Color c = initialColor;
            if (value != DependencyProperty.UnsetValue)
                c.B = System.Convert.ToByte(value);
            return c;
        }
    }

    [ValueConversion(typeof(double[]), typeof(SolidColorBrush))] 
    public class ColorFromRGB : MarkupExtension, IMultiValueConverter
    {
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return this;
        }
        public object Convert(object[]  values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            byte r = 0,g=0,b=0;
            if (values[0]!=DependencyProperty.UnsetValue)
                r = System.Convert.ToByte(values[0]);
            if (values[1] != DependencyProperty.UnsetValue)
                g = System.Convert.ToByte(values[1]);
            if (values[2] != DependencyProperty.UnsetValue)
            b = System.Convert.ToByte(values[2]);
            Color c = Color.FromArgb(255,r, g, b);
            return new SolidColorBrush(c) ;
        }
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new Exception();
        }
    }
     



}
